package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class AddItemActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText3;
    private EditText editText4;
    private EditText editText5;
    private EditText editText6;
    private Button button3;
    private Button button4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item);
        initView();
    }

    private void initView() {
        editText3 = (EditText) findViewById(R.id.editText3);
        editText4 = (EditText) findViewById(R.id.editText4);
        editText5 = (EditText) findViewById(R.id.editText5);
        editText6 = (EditText) findViewById(R.id.editText6);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);

        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button3:
                //获取前台传过来的数据
                String name = editText3.getText().toString();
                String desc = editText4.getText().toString();
                String remark = editText5.getText().toString();
                String price = editText6.getText().toString();
                //将数据打包成map对象
                Map<String,String> map = new HashMap<>();
                map.put("itemName",name);
                map.put("itemDesc",desc);
                map.put("itemRemark",remark);
                map.put("initPrice",price);
                map.put("kindId","1");
                map.put("avail","1");
                //发送数据
                try {
                    String result = OkHttpUtil.postRequest(
                            "http://172.18.85.254:8080/auction/api/items", map);
                    Toast.makeText(this,result,Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            case R.id.button4:
                Intent intent = new Intent(this,ItemActivity.class);
                startActivity(intent);
                break;
        }
    }

}
